package com.youfan.control;

import com.youfan.entity.ProductattributeCa;
import com.youfan.page.CommonPage;
import com.youfan.re.Result;
import com.youfan.service.ProductAttrCtService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping(value = "productAttr/productAttributeCt")
public class ProductAttributeCtControl {

    @Autowired
    private ProductAttrCtService productAttrCtService;

    @RequestMapping(method = RequestMethod.POST,value = "add")
    @ResponseBody
    public Result add(@RequestBody ProductattributeCa youfanProductattributeCa)
    {
        try {
            int sucessnums = productAttrCtService.insertOne(youfanProductattributeCa);
            return Result.success(sucessnums,"添加成功");
        }catch (Exception e){
            e.printStackTrace();
            return Result.fail("添加失败");
        }
    }

    @RequestMapping(method = RequestMethod.POST,value = "list")
    @ResponseBody
    public Result list(@RequestBody ProductattributeCa youfanProductattributeCa){
        try {
            if (youfanProductattributeCa.getPageNum() == null) {
                youfanProductattributeCa.setPageNum(1);
            }
            if (youfanProductattributeCa.getPageSize() == null) {
                youfanProductattributeCa.setPageSize(5);
            }
            CommonPage<ProductattributeCa> resultdata = productAttrCtService.queryByCondtion(youfanProductattributeCa);

            return Result.success(resultdata, "查询成功");
        }catch (Exception e){
            e.printStackTrace();
            return Result.fail("查询失败"+e.getMessage());
        }
    }


    @RequestMapping(method = RequestMethod.POST,value = "view")
    @ResponseBody
    public Result view(@RequestBody  ProductattributeCa youfanProductattributeCa)
    {
        try {
            ProductattributeCa youfanProductattributeCa1 = productAttrCtService.findByid(youfanProductattributeCa);
            return Result.success(youfanProductattributeCa1,"查询单个对象成功");
        }catch (Exception e){
            e.printStackTrace();
            return Result.fail("查询单个对象失败");
        }
    }

    @RequestMapping(method = RequestMethod.POST,value = "update")
    @ResponseBody
    public Result update(@RequestBody ProductattributeCa youfanProductattributeCa)
    {
        try {

            ProductattributeCa youfanProductattributeCa1 =  productAttrCtService.findByid(youfanProductattributeCa);
            youfanProductattributeCa1.setName(youfanProductattributeCa.getName());
            youfanProductattributeCa1.setAttributedesc(youfanProductattributeCa.getAttributedesc());
            int result = productAttrCtService.updateOne(youfanProductattributeCa1);
            return Result.success(result,"更新成功");
        }catch (Exception e){
            e.printStackTrace();
            return Result.fail("更新失败");
        }
    }


}
